package com.burgeon.backend.EnterpriseWeChat.rest.Gtasks;

import com.burgeon.backend.EnterpriseWeChat.entity.UsefulExpressions;
import com.burgeon.backend.EnterpriseWeChat.entity.Vip;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class getVerbleTrickByGuider extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public getVerbleTrickByGuider(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<UsefulExpressions> getData(String empId, String type) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(empId,type), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");

        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<UsefulExpressions> usefulExpressions= new ArrayList<>();
        if(jsonArray.length()>0){
            for (int i = 0; i <jsonArray.length() ; i++) {
                UsefulExpressions usefulExpressions1 = new UsefulExpressions();
                usefulExpressions1.setRviSitCount(jsonArray.getJSONArray(i).getString(0));
                usefulExpressions1.setId(jsonArray.getJSONArray(i).getString(1));
               usefulExpressions.add(usefulExpressions1);
            }
        }

        return usefulExpressions;
    }

    private MultiValueMap Data(String empId,String type) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段
        colums.add("RVISIT_CONTENT");//
        colums.add("ID");//

        params.put("columns", colums);
        params.put("table", "APP_VIP_RVISITLAE");//表名
        params.put("count", "true");


        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        expr1.put("column","'HR_EMPLOYEE_ID'");
        String ac = "'"+empId+"'";
        expr1.put("condition",ac);

        HashMap expr2 = new HashMap();
        expr2.put("column","RVISITLAE_TYPE");
        expr2.put("condition","'"+type+"'");

        HashMap expr3 = new HashMap();
        HashMap expr4 = new HashMap();
        HashMap expr5 = new HashMap();
        HashMap expr6 = new HashMap();

        expr3.put("combine", "or");
        expr3.put("expr1", expr1);
        expr3.put("expr2", expr2);

        expr4.put("column", "RVISITLAE_TYPE");
        expr4.put("condition", "'"+type+"'");

        expr5.put("column", "HR_EMPLOYEE_ID");
        expr5.put("condition", "''");

        expr6.put("combine", "and");
        expr6.put("expr1", expr4);
        expr6.put("expr2", expr5);

        paramss.put("combine", "and");
        paramss.put("expr1", expr3);
        paramss.put("expr2", expr6);

        params.put("params", paramss);
        System.err.println(params);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        getVerbleTrickByGuider g = new getVerbleTrickByGuider("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("","6"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
